#include "Window.h"
#include "EDP.h"
#include "stdio.h"
#include "Delay.h"
WM_HWIN hMea;
#define NUM_YPOS 120
#define NUM_YOFF 25
#define NUM_XBLANK 58
#define ID_GRAPH_WAVE (GUI_ID_USER+0)
#define ID_BUTTON_EXIT (GUI_ID_USER+1)
#define ID_BUTTON_UPLOAD (GUI_ID_USER+2)
#define ID_TEXT (GUI_ID_USER+3)
#define ID_FRAM (GUI_ID_USER+4)
#define ID_FRAM_TEXT (GUI_ID_USER+5)

bool ExitMesuare;
TEXT_Handle hTextMesuare[8];
BUTTON_Handle hButtonExitMesuare,hButtonUpload;
GRAPH_DATA_Handle hData;
static void _cbMea(WM_MESSAGE* pMsg)
{
	static GRAPH_Handle hGraph;
	static GRAPH_SCALE_Handle hScale;
	switch(pMsg->MsgId)
	{
		case WM_CREATE:
		{
			ExitMesuare = false;
			hGraph = GRAPH_CreateEx(6, 6, 308, 105, pMsg->hWin,
			                        WM_CF_SHOW, 0, ID_GRAPH_WAVE);
			GRAPH_SetBorder(hGraph, 30, 0, 0, 0);
			hData = GRAPH_DATA_YT_Create(GUI_WHITE, 512, nullptr, 0);
			GRAPH_AttachData(hGraph, hData);
			hScale = GRAPH_SCALE_Create(15, GUI_TA_HCENTER | GUI_TA_BOTTOM,
			                            GRAPH_SCALE_CF_VERTICAL, 15);
			GRAPH_SetColor(hGraph, GUI_BLACK, GRAPH_CI_BORDER);
			GRAPH_AttachScale(hGraph, hScale);
			GRAPH_SetGridDistY(hGraph, 15);
			//GRAPH_SetGridVis(hGraph, 1);
			GRAPH_SCALE_SetFactor(hScale, 2048 / 89);
			
			
			hButtonExitMesuare = BUTTON_CreateEx(5, 130, 20, 20, pMsg->hWin,WM_CF_SHOW, 0, ID_BUTTON_EXIT);
			BUTTON_SetText(hButtonExitMesuare, "<");
			
			hButtonUpload = BUTTON_CreateEx(5, 210, 60, 20, pMsg->hWin,0, 0, ID_BUTTON_UPLOAD);
			BUTTON_SetText(hButtonUpload, "Upload");
			
			
			hTextMesuare[0] = TEXT_CreateEx(NUM_XBLANK + 24, NUM_YPOS, 48, 32, pMsg->hWin,
			                      WM_CF_SHOW, TEXT_CF_RIGHT, ID_TEXT, "5");//--

			hTextMesuare[1] = TEXT_CreateEx(NUM_XBLANK * 2 + 72, NUM_YPOS, 72, 32, pMsg->hWin,
			                      WM_CF_SHOW, TEXT_CF_RIGHT, ID_TEXT, "");//--

			hTextMesuare[2] = TEXT_CreateEx(NUM_XBLANK, NUM_YPOS + NUM_YOFF + 32, 72, 32, pMsg->hWin,
			                      WM_CF_SHOW, TEXT_CF_RIGHT, ID_TEXT, "");//---

			hTextMesuare[3] = TEXT_CreateEx(NUM_XBLANK * 2 + 72, NUM_YPOS + NUM_YOFF + 32, 72, 32, pMsg->hWin,
			                      WM_CF_SHOW, TEXT_CF_RIGHT, ID_TEXT, "");//--
			for(int i=0;i<4;i++)
				TEXT_SetFont(hTextMesuare[i], &GUI_FontD24x32);

			hTextMesuare[4] = TEXT_CreateEx(NUM_XBLANK + 24 - 32, NUM_YPOS + 35, 80, 16, pMsg->hWin,
			                           WM_CF_SHOW, TEXT_CF_RIGHT, ID_TEXT, "");//Rate

			hTextMesuare[5] = TEXT_CreateEx(NUM_XBLANK * 2 + 72 - 7, NUM_YPOS + 35, 80, 16, pMsg->hWin,
			                           WM_CF_SHOW, TEXT_CF_RIGHT, ID_TEXT, "");//Spo2

			hTextMesuare[6] = TEXT_CreateEx(NUM_XBLANK - 8, NUM_YPOS + NUM_YOFF + 32 + 35, 80, 16, pMsg->hWin,
			                           WM_CF_SHOW, TEXT_CF_RIGHT, ID_TEXT, "");//High

			hTextMesuare[7] = TEXT_CreateEx(NUM_XBLANK * 2 + 72 - 8, NUM_YPOS + NUM_YOFF + 32 + 35, 80, 16, pMsg->hWin,
			                           WM_CF_SHOW, TEXT_CF_RIGHT, ID_TEXT, "");//Low
			for(int i=4;i<8;i++)
				TEXT_SetFont(hTextMesuare[i], &UTF_8);
				
				xSemaphoreGive(StartMesuare);
/*
				TEXT_SetText(hTextMesuare[0], "65");
        TEXT_SetText(hTextMesuare[1], "98");
        TEXT_SetText(hTextMesuare[2], "121");
        TEXT_SetText(hTextMesuare[3], "62");
*/
		}
		break;
		
		case WM_PAINT:
			GUI_SetBkColor(GUI_WHITE);
			GUI_Clear();
			GUI_SetColor(GUI_BLACK);
			break;
		
		case WM_NOTIFY_PARENT:
		{
			int NCode = pMsg->Data.v;
			int Id = WM_GetId(pMsg->hWinSrc);
			switch(Id)
			{
				case ID_BUTTON_EXIT://退出按钮收到消息
					if(NCode == WM_NOTIFICATION_RELEASED)//按下后抬起
					{
						WM_DeleteWindow(pMsg->hWin);//关闭当前窗口
						CreateMainWindow();//创建主窗口
					}
					break;
					
				case ID_BUTTON_UPLOAD://上传按钮收到消息
					if(NCode == WM_NOTIFICATION_RELEASED)//按下后抬起
					{
						FRAMEWIN_Handle hFra = FRAMEWIN_CreateEx(100, 90, 110, 60, pMsg->hWin, WM_CF_SHOW, FRAMEWIN_CF_MOVEABLE | FRAMEWIN_CF_ACTIVE, ID_FRAM, "", nullptr);
						TEXT_Handle th = TEXT_CreateEx(0, 0, 100, 38, WM_GetClientWindow(hFra), WM_CF_SHOW, TEXT_CF_HCENTER | TEXT_CF_VCENTER, ID_FRAM_TEXT, cn[8]);
						TEXT_SetFont(th,&UTF_8);
						
						uint8_t *buf = new uint8_t[128];
						char buf2[64];
						snprintf(buf2,sizeof(buf2),",;%s,%d;%s,%d;%s,%d;%s,%d",
							cn[4],Data[0],cn[5],Data[1],
							cn[6],Data[2],cn[7],Data[3]);
						uint16_t len = strlen(buf2);
						uint16_t HeadLen = EDP_SaveData(&buf,nullptr,0,Custom_String,len);
						memcpy(buf+HeadLen,buf2,strlen(buf2));//合并数据包
						static UpLoadPacket_t Pack;
						Pack.Data = buf;
						Pack.isDelete = true;
						Pack.Length = HeadLen+len;
						xSemaphoreTake(uploadSucc, 0);
						xQueueSend(NetworkUpload,&Pack,portMAX_DELAY);//将数据包写入发送队列
						Delay.ms(1000);
						auto res = xSemaphoreTake(uploadSucc, 200);
						FRAMEWIN_AddCloseButton(hFra, FRAMEWIN_BUTTON_RIGHT, 0);
						TEXT_SetText(th, cn[res == pdPASS ? 9 : 10]);
					}
					break;
			}
		}
		break;
		
		case WM_DELETE:
			ExitMesuare = true;
			break;
		default:
			WM_DefaultProc(pMsg);
			break;
	}
}
void CreateMeasureWindow()
{
	hMea = WM_CreateWindow(0, 0, LCD_GetXSize(), LCD_GetYSize(),
	                       WM_CF_SHOW | WM_CF_MEMDEV_ON_REDRAW, _cbMea, 0);
}
